package com.stylefeng.guns.wx.modular.auth.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.wx.common.persistence.dao.UserMapper;
import com.stylefeng.guns.wx.common.persistence.dao.UserNovelMapper;
import com.stylefeng.guns.wx.common.persistence.model.User;
import com.stylefeng.guns.wx.common.persistence.model.UserNovel;
import com.stylefeng.guns.wx.modular.auth.service.IWxAuthService;
import com.stylefeng.guns.wx.modular.auth.util.MessageUtil;
import com.stylefeng.guns.wx.modular.scheduled.util.webmagic.WebmagicQiDianUtil;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.Spider;

import javax.annotation.Resource;
import java.util.*;

@Service
public class WxAuthServiceImpl implements IWxAuthService {

    @Resource
    UserNovelMapper userNovelMapper;

    @Resource
    UserMapper userMapper;

    @Override
    public String emailBind(String openid, String email) {
        System.out.println(openid);
        User user = new User();
        user.setOpenid(openid);
        user.setEmail(email);
        user.setCreateTime(new Date());
        user.setUseFlag("0");
        int i = save(user);
        boolean flag = false;
        if(i>0){
            flag=true;
        }
        return MessageUtil.firstMenu(flag,email);
    }

    @Override
    public Integer save(User user) {
        User u = userMapper.selectById(user.getOpenid());
        if(null==u){
            return userMapper.insert(user);
        }else{
            return userMapper.updateById(u);
        }
    }

    @Override
    public String updateUseFlag(String openid) {
        User u = userMapper.selectById(openid);
        if(null==u){
            return "请先绑定邮箱";//错误不存在绑定过邮箱的用户
        }else{
            u.setUseFlag("1");
            int i = userMapper.updateById(u);
            if(i>0){
                return "绑定成功,请关注您的书籍吧!";
            }else{
                return "绑定失败,未知错误!";
            }
        }
    }

    @Override
    public String novelBind(String openid,String novelId) {
        User u = userMapper.selectById(openid);
        if(null==u){
            return "关注失败!请先绑定邮箱";
        }
        UserNovel userNovel = new UserNovel();
        userNovel.setId(UUID.randomUUID().toString());
        userNovel.setEmail(u.getEmail());
        userNovel.setUserOpenId(openid);
        userNovel.setNovelId(novelId);
        userNovel.setNovelWebId("1");
        userNovel.setSendFlag("0");
        userNovel.setSendType("1");
        userNovel.setDeleteFlag("00");
        Wrapper<UserNovel> wrapper = new EntityWrapper<>();
        wrapper = wrapper.eq("user_open_id", userNovel.getUserOpenId())
                .eq("novel_id",userNovel.getNovelId());
        List<UserNovel> userNovelList = userNovelMapper.selectList(wrapper);
        if(null!=userNovelList&&userNovelList.size()>0){
            StringBuffer sb2 = new StringBuffer();
            sb2.append("您已关注小说<<")
                    .append(userNovelList.get(0).getNovelTitle())
                    .append(">>!");
            return sb2.toString();
        }
        Spider spider = Spider.create(new WebmagicQiDianUtil());
        StringBuffer sb = new StringBuffer();
        sb.append("https://book.qidian.com/info/")
                .append(userNovel.getNovelId());
        spider.addUrl(sb.toString());
        spider.run();
        Map<String,String> map = WebmagicQiDianUtil.maps.get(sb.toString());
        if(map==null){
            return  MessageUtil.secondMenu(false,novelId);
        }
        String title = map.get("title");
        String latestChapter = map.get("latestChapter");
        userNovel.setNovelTitle(title);
        userNovel.setNovelLatestChapter(latestChapter);
        userNovel.setCreateTime(new Date());
        int  i = userNovelMapper.insert(userNovel);
        if(i>0){
            return MessageUtil.secondMenu(true,title);
        }
        if(null!=userNovelList&&userNovelList.size()>=10){
            return "您关注的小说已超过上限!";
        }
        return MessageUtil.secondMenu(false,novelId);
    }

    @Override
    public String delete(String openid,String novelId) {
        Wrapper<UserNovel> wrapper = new EntityWrapper<>();
        wrapper = wrapper.eq("user_open_id", openid)
                .eq("novel_id",novelId);
        int i = userNovelMapper.delete(wrapper);
        if(i>0){
            return "取消关注成功!";
        }
        return "取消关注失败";
    }

    @Override
    public String novelList(String openid) {
        Wrapper<UserNovel> wrapper = new EntityWrapper<>();
        wrapper = wrapper.eq("user_open_id", openid);
        List<UserNovel> list = userNovelMapper.selectList(wrapper);
        StringBuffer sb = new StringBuffer();
        if(null==list||list.size()<=0){
            return "您还未关注,赶快去关注吧!";
        }
        for (int i = 0; i < list.size(); i++) {
            String title = list.get(i).getNovelTitle();
            sb.append(i+1).append(".").append(title).append("\n");
        }
        return sb.toString();
    }
}
